package com.wandousoushu.jiusen.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wandousoushu.jiusen.R;
import com.wandousoushu.jiusen.bean.BookMailBean;
import com.wandousoushu.jiusen.booksource.bean.BookBean;

import java.util.List;
import java.util.Random;

public class BookMailAdapter extends RecyclerView.Adapter<BookMailAdapter.Vh>  {

    private Context mContext;
    private List<BookMailBean> mBookBeanList;
    private LayoutInflater mInflater;
    private ActionListener mActionListener;
    private View.OnClickListener mOnClickListener;
    private int[] colors={R.color.random_color_1,R.color.random_color_2,R.color.random_color_3,R.color.random_color_4,R.color.random_color_5,R.color.random_color_6,R.color.random_color_7};

    public BookMailAdapter(Context mContext, List<BookMailBean> bookBeanList) {
        this.mContext = mContext;
        this.mBookBeanList=bookBeanList;
        mInflater = LayoutInflater.from(mContext);
        this.notifyDataSetChanged();
        mOnClickListener=new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Object tag = view.getTag();
                if(tag!=null){
                    int position = (int) tag;
                    if (mActionListener != null) {
                        mActionListener.onItemClick(mBookBeanList.get(position));
                    }
                }
            }
        };
    }

    public void setActionListener(ActionListener listener) {
        mActionListener = listener;
    }

    @Override
    public BookMailAdapter.Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.item_bookmail_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(BookMailAdapter.Vh holder, int position) {
        holder.setData(mBookBeanList.get(position), position);
    }

    @Override
    public int getItemCount() {
        return mBookBeanList.size();
    }

    class Vh extends RecyclerView.ViewHolder {
        LinearLayout itemLinearLayout;
        TextView nameTextView;

        public Vh(View itemView) {
            super(itemView);
            itemLinearLayout=itemView.findViewById(R.id.item_bookmail);
            nameTextView = (TextView) itemView.findViewById(R.id.tv_bookmail_name);
            itemView.setOnClickListener(mOnClickListener);
        }

        @SuppressLint("ResourceAsColor")
        void setData(BookMailBean bean, int position) {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            //此处相当于布局文件中的Android:layout_gravity属性
            Random random = new Random();
            int radomData=random.nextInt(100);
            String name=bean.getName();
            if(name.length()>10){
                lp.gravity = Gravity.LEFT;
                lp.setMargins(radomData+20,10,0,0);
            }else if(position%2==1){
                lp.gravity = Gravity.RIGHT;
                lp.setMargins(radomData+500,10,0,0);
            }else if(position%2==0){
                lp.gravity = Gravity.LEFT;
                lp.setMargins(radomData+20,10,0,0);
            }
            itemLinearLayout.setLayoutParams(lp);
            itemView.setTag(position);
            nameTextView.setTextColor(mContext.getResources().getColor(colors[radomData%7]));
            nameTextView.setText(bean.getName());
        }
    }


    public void refresh(List<BookMailBean> newList) {
        mBookBeanList.removeAll(mBookBeanList);
        mBookBeanList.addAll(newList);
        notifyDataSetChanged();
    }


    public interface ActionListener {
        void onItemClick(BookMailBean bookBean);
    }
}
